/*判断用户是否合法输入*/
var checkFormValidity = true;

function checkFormValidityAll() {
    checkFormValidityPhoneNumber();
    checkFormValidityAppointmentDate();
    checkFormValidityIssueDetails();
    checkIsSubmit();
}

function checkFormValidityPhoneNumber() {
    // 判断手机号是否为正常数字且为11位数字
    if (isNaN(document.getElementById("phoneNumber").value) || !/^\d{11}$/.test(document.getElementById("phoneNumber").value)) {
        alert("请输入正确的手机号");
        checkFormValidity = false;
    }
}

function checkFormValidityAppointmentDate() {
    var dateParts = document.getElementById("appointmentDate").value.split("-");

    var year = parseInt(dateParts[0]);
    var month = parseInt(dateParts[1]);
    var day = parseInt(dateParts[2]);

    var date = new Date(year, month - 1, day);
    // 判断转换后的日期是否仍然和输入的年月日相同
    var isExistingDate = date.getFullYear() === year && date.getMonth() === month - 1 && date.getDate() === day;

    // 判断日期是否在今天之后
    var date1 = new Date();

    date1.setTime(date1.getTime() + 8 * 60 * 60 * 1000);

    var year1 = date1.getFullYear().toString();
    var month1 = (date1.getMonth() + 1).toString().padStart(2, '0');
    var day1 = date1.getDate().toString().padStart(2, '0');

    var beijingDate = year1 + "-" + month1 + "-" + day1;

    var beijingNum = Number(beijingDate.replace(/-/g, ""));
    var bookingNum = Number(document.getElementById("appointmentDate").value.replace(/-/g, ""));

    // 判断日期是否格式为"yyyy-MM-dd"，在今天之后且日期存在
    if (!/^\d{4}-\d{2}-\d{2}$/.test(document.getElementById("appointmentDate").value) || bookingNum < beijingNum || !isExistingDate) {
        alert("请输入正确的日期");
        checkFormValidity = false;
    }
}

function checkFormValidityIssueDetails() {
    // 判断问题描述是否多于400个字
    if (document.getElementById("issueDetails").value.length > 400) {
        alert("问题描述不能多于400个字");
        checkFormValidity = false;
    }

    // 判断问题描述是否为空
    if (document.getElementById("issueDetails").value.length < 1) {
        alert("问题描述不能为空");
        checkFormValidity = false;
    }
}

function checkIsSubmit() {
    if (checkFormValidity) {
        event.preventDefault();
        sendMaintainFormData();
    } else {
        document.getElementById("myForm").action = "maintain.html";
    }
}

function sendMaintainFormData() {
    var appointmentDate = document.getElementById("appointmentDate").value;
    var issueType = document.getElementById("issueType").value;
    var issueDetails = document.getElementById("issueDetails").value;
    var phoneNumber = document.getElementById("phoneNumber").value;

    var repairRecord = {
        appointmentDate: appointmentDate,
        issueType: issueType,
        issueDetails: issueDetails,
        phoneNumber: phoneNumber
    };

    var xhr = new XMLHttpRequest();
    xhr.open("POST", "/maintain", true);
    xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
    xhr.onreadystatechange = function () {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status === 200) {
                alert(xhr.responseText);
                window.location.href = "maintain.html";
            } else {
                alert("Error: " + xhr.status);
            }
        }
    };
    xhr.send(JSON.stringify(repairRecord));
}